package core;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.HashMap;

public class GenerObject
{
	private static	HashMap<String, HashMap<Integer, HashMap<String, HashMap<String, String>>>>	_lightObj = new HashMap<String, HashMap<Integer,HashMap<String,HashMap<String,String>>>>();
	private static Object obj = null;
	
	private static void		prepareLightObject(HashMap<Integer,HashMap<String,String>> field, String nameField, String name){
		//System.out.println(_lightObj);
		
		for (int index=0;index< field.size();index++){
			/* 4eme palier */
			HashMap<String, String> 	l = new HashMap<String, String>();
			l.put("value", field.get(index).get("value"));
			l.put("type", field.get(index).get("type"));
			
			/* 3eme palier */
			HashMap<String, HashMap<String, String>> l_ = null;
			if (_lightObj.containsKey(name) && _lightObj.get(name).containsKey(index))
				l_ = _lightObj.get(name).get(index);
			else
				l_ = new HashMap<String, HashMap<String,String>>();
			l_.put(nameField, l);
			
			/* 2ond palier */
			HashMap<Integer, HashMap<String, HashMap<String, String>>> l__ = null;
			if (_lightObj.containsKey(name))
				l__ = _lightObj.get(name);
			else
				l__ = new HashMap<Integer, HashMap<String,HashMap<String,String>>>();
			l__.put(index, l_);
			
			/* 1er palier */
			_lightObj.put(name, l__);
		}
	}
	
	public static Object	getItBaby(HashMap<String, HashMap<String,HashMap<Integer,HashMap<String,String>>>> schema)
	{
		for (String key_ : schema.keySet())
		{
			try
			{
				Class c = Class.forName("model."+key_.replaceFirst(".",(key_.charAt(0)+"").toUpperCase()));
				HashMap<String, Object>	light = new HashMap<String, Object>();
		        Object instance = c.newInstance();
		        for (String key : schema.get(key_).keySet()){
		        	Field f = null;
		        	if (key.split("_")[0].trim().compareTo(key_.trim())!=0){
		        		System.out.println("Introspection et remplissage de "+key.split("_")[0]+" avec le champs "+key.split("_")[1]);
		        		prepareLightObject(schema.get(key_).get(key), key.split("_")[1], key.split("_")[0]);
		        	}else{
			        	try{
				        	f = c.getDeclaredField(key.split("_")[1]);
			        	}catch(Exception e){
				        	f = c.getField(key.split("_")[1]);
			        	}
			        	
				        f.setAccessible(true);
				        
				        if (schema.get(key_).get(key).get(0).get("type") == "java.sql.Timestamp"){
				        	f.set(instance, Timestamp.valueOf(schema.get(key_).get(key).get(0).get("value")));
				        }else if (schema.get(key_).get(key).get(0).get("type") == "java.lang.Integer"){
				        	Integer t = new Integer(Integer.parseInt(schema.get(key_).get(key).get(0).get("value")));
				        	f.set(instance, t);
				        }else
				        	f.set(instance, schema.get(key_).get(key).get(0).get("value"));
				        
				        System.out.println("Field : "+f.getName()+" | Value : "+f.get(instance));
				        f.setAccessible(false);
		        	}
		        }
		        obj = instance;
			}
			catch(Exception e){System.out.println(e.toString());}
		}
		
//		System.out.println("LightObject ::\r\n\r\n"+_lightObj);
		return(obj);
	}
	
	private	GenerObject(){}
}
